﻿package com.snakedickgames.common.sandy 
{
    import com.pblabs.engine.debug.Logger;
    import flash.events.Event;
    import sandy.core.scenegraph.Shape3D;
    import sandy.core.scenegraph.TransformGroup;
    import sandy.parser.IParser;
    import sandy.parser.ParserStack;
    import sandy.parser.Parser;
    
    
    public class SandyShapeParserNode extends SandyTransformable
    {
        public var modelFiles:Array;
        public var modelFormat:String = Parser.ASE;
        protected var shapes:Array = new Array();
        protected var parserStack:ParserStack = new ParserStack();
        
        public function get shape3D():Shape3D 
        {
            return _node as Shape3D;
        }
        
        override protected function onReset():void 
        {
            if (!node && scene && !_loadingModels)
            {
                parseModels();
            }
            super.onReset();
        }
        
        protected function parseModels():void
        {
            for (var i:String in modelFiles)
            {
                var parser:IParser = Parser.create(modelFiles[i], modelFormat);
                parserStack.add(i, parser);
            }
            _loadingModels = true;
            parserStack.addEventListener(ParserStack.COMPLETE, parserComplete);
            parserStack.start();
        }
        
        protected function parserComplete(event:Event):void
        {
            node = new TransformGroup(nodeName);
            for (var i:String in modelFiles)
            {
                var shape:Shape3D = parserStack.getGroupByName(i).children[0] as Shape3D;
                node.addChild(shape);
                shapes[i] = shape;
                if (appearance)
                    node.appearance = appearance;
            }
        }
        
        protected var _loadingModels:Boolean = false;
    }

}